<!--
 * @Author: LiYuan
 * @Date: 2021-03-31 16:25:52
 * @LastEditTime: 2021-06-17 13:19:39
 * @LastEditors: LiYuan
 * @Description: 暂停事件
 * You build it, You run it.
-->
<template>
  <base-modal :title="$t('event.detail.components.dialog.text42')" :show.sync="visible" width="600px" class="event-pause-dialog">
    <div class="base-modal-content">
      <el-form label-position="top" :model="pauseForm" ref="pauseRef" :rules="rules">
        <el-form-item :label="$t('task.pauseReason')" prop="pauseReason">
         <el-input v-model="pauseForm.pauseReason" type="textarea" :placeholder="$t('event.detail.components.dialog.text43')" maxlength="500"></el-input>
        </el-form-item>
      </el-form>
    </div>
    <div slot="footer">
      <el-button @click="visible = false">{{$t('common.base.close')}}</el-button>
      <el-button type="primary" @click="handlePause" :loading="loading">{{$t('common.base.usualStatus.paused')}}</el-button>
    </div>
  </base-modal>
</template>

<script>
import * as EventApi from '@src/api/EventApi.js';
export default {
  name: 'event-pause-dialog',
//   inject: ['initData'],
  props:{
    workEvent: {
      type: Object,
      default: () => {}
    },
    getVipApproveFunction: {
      type: Function,
      default: (() => {})
    }
  },
  data() {
    return {
      pauseForm: {
        pauseReason: ''
      },
      rules: {
        pauseReason: [{ required: true, message: this.$t('event.detail.components.dialog.text43'), trigger: 'blur' }]
      },
      visible: false,
      loading: false
    }
  },
  methods: {
    openDialog() {
      this.visible = true;
    },
    handlePause() {
      this.$refs['pauseRef'].validate(async (vali) => {
        if (vali) {
          const params = {
            id: this.workEvent.id,
            reason: this.pauseForm.pauseReason
          }
          this.loading = true

          // 判断是否有高级审批(有审批或者接口报错，return)
          try {
            const result = await this.getVipApproveFunction('allowPause', params)
            if (result.needVipApprove) {
              this.visible = false
              this.$emit('needVipApprove', result);
              return;
            }
          } catch (e) {
            console.error(e)
            return;
          } finally {
            this.loading = false;
          }

          EventApi.eventPause(params)
              .then(res => {
                if (res.status == 0) {
                  window.location.href = `${this.$resourcePrefix}/event/view/${this.workEvent.id}`;
                } else {
                  // TODO 需要审批判断改成errorCode
                  if (res.message == 'needwait') {
                    this.$message.warning(this.$t('event.detail.components.eventButtonGroup.text13'))
                  } else if (Number(res.errorCode == 10003)) {
                    this.visible = false
                    this.$emit('needApprove', res.data)
                  } else {
                    this.$notify({
                      title: this.$t('common.base.error'),
                      message: res.message,
                      type: 'error'
                    });
                  }
                }
              })
              .finally(() => {
                this.loading = false
              })
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.base-modal-content {
  padding: 20px;
}
.is-required:before {
  content: '';
}
</style>